package com.efast.dmom.mgmt.controller.les;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TwAdjDetails;
import com.efast.dmom.mgmt.entity.les.TwAdjHeader;
import com.efast.dmom.mgmt.service.les.TwAdjDetailsService;
import com.efast.dmom.mgmt.service.les.TwAdjHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

/**
 * @author chlp
 */
@Controller
@RequestMapping(value = "/twAdjHeader")
@Api(description = "物流执行管理-->库存调整-->库存调整单【header】")
public class TwAdjHeaderController {

	private final TwAdjHeaderService twAdjHeaderService;
	private final TwAdjDetailsService twAdjDetailsService;

	@Autowired
	public TwAdjHeaderController(TwAdjHeaderService twAdjHeaderService, TwAdjDetailsService twAdjDetailsService) {
		this.twAdjHeaderService = twAdjHeaderService;
		this.twAdjDetailsService = twAdjDetailsService;
	}

	/**
	 *
	 * @param twAdjHeader
	 *            twAdjHeader
	 */
	@ApiOperation(value = "【新增】", notes = "请求参数：")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody ResultBean confirmPicking(@RequestBody TwAdjHeader twAdjHeader) {
		ResultBean result = new ResultBean(true, "'");
		twAdjHeaderService.insertNonEmptyTwAdjHeader(twAdjHeader);
		return result;
	}

	/**
	 *
	 * @param twAdjHeader
	 *            twAdjHeader
	 */
	@ApiOperation(value = "【发布】", notes = "请求参数：")
	@RequestMapping(value = "/release", method = RequestMethod.POST)
	public @ResponseBody ResultBean release(@RequestBody TwAdjHeader twAdjHeader) {
		ResultBean result = new ResultBean(true, "'");
		// 单据状态:0新单,1发布,2完成,3关闭,4部分调整
		twAdjHeader.setSheetStatus("1");
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("tw_adj_header_id", twAdjHeader.getTwAdjHeaderId()));
		twAdjHeaderService.updateNonEmptyTwAdjHeader(twAdjHeader,assist);
		List<TwAdjDetails> twAdjDetailsList = twAdjDetailsService.selectTwAdjDetails(
				new Assist(Assist.andEq("tw_adj_header_id", twAdjHeader.getTwAdjHeaderId())));
		for (TwAdjDetails twAdjDetails : twAdjDetailsList) {
			twAdjDetails.setLineStatus("1");
			twAdjDetailsService.updateNonEmptyTwAdjDetailsById(twAdjDetails);
		}
		return result;
	}

	/**
	 *
	 * @param twAdjHeader
	 *            twAdjHeader
	 */
	@ApiOperation(value = "【执行】", notes = "请求参数：")
	@RequestMapping(value = "/execute", method = RequestMethod.POST)
	public @ResponseBody ResultBean execute(@RequestBody TwAdjHeader twAdjHeader) {
		ResultBean result = new ResultBean(true, "'");
		TwAdjHeader twadjheader = twAdjHeaderService.selectTwAdjHeaderById(twAdjHeader.getTwAdjHeaderId());
		if (!"1".equals(twadjheader.getSheetStatus())) {
			throw new ServiceException("只有发布后，才能执行库存调整");
		}
		// 单据状态:0新单,1发布,2完成,3关闭,4部分调整
		twAdjHeader.setSheetStatus("2");
		twAdjHeaderService.updateNonEmptyTwAdjHeaderById(twAdjHeader);
		List<TwAdjDetails> twAdjDetailsList = twAdjDetailsService.selectTwAdjDetails(
				new Assist(Assist.andEq("tw_adj_header_id", twAdjHeader.getTwAdjHeaderId())));
		for (TwAdjDetails twAdjDetails : twAdjDetailsList) {
			twAdjDetails.setLineStatus("2");
			twAdjDetailsService.updateNonEmptyTwAdjDetailsById(twAdjDetails);
		}
		return result;
	}

	/**
	 *
	 * @param twAdjHeader
	 *            twAdjHeader
	 */
	@ApiOperation(value = "【关闭】", notes = "请求参数：")
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	public @ResponseBody ResultBean close(@RequestBody TwAdjHeader twAdjHeader) {
		ResultBean result = new ResultBean(true, "'");
		// 单据状态:0新单,1发布,2完成,3关闭,4部分调整
		twAdjHeader.setSheetStatus("3");
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("tw_adj_header_id", twAdjHeader.getTwAdjHeaderId()));
		twAdjHeaderService.updateNonEmptyTwAdjHeader(twAdjHeader,assist);
		List<TwAdjDetails> twAdjDetailsList = twAdjDetailsService.selectTwAdjDetails(
				new Assist(Assist.andEq("tw_adj_header_id", twAdjHeader.getTwAdjHeaderId())));
		for (TwAdjDetails twAdjDetails : twAdjDetailsList) {
			twAdjDetails.setLineStatus("1");
			twAdjDetailsService.updateNonEmptyTwAdjDetailsById(twAdjDetails);
		}
		return result;
	}

}
